JObj.use('JAnimate');
!JObj.Plugin && (JObj.Plugin = {});
JObj.Plugin.JAccordion = {};
(function($){

    var JAccordion = function(body,width,height){
        var self = this;
        var vars = {
            skin:'JAccordionDefault',
            width: JObj.isRate(width) ? width : (!isNaN(width) ? width + "px" : "150px"),
            height: JObj.isRate(height) ? height : (!isNaN(height) ? height : 100),
            panelHeight:null,
            opening:false,
            mouseoverInterval : null
        };
        var objs = {
            body:JObj.$(body) || document.body,
            outline:null,
            ctxs:[],
            labels:[],
            opened:null
        };

        self.onOpen = null;

        var createOutline = function(){
            objs.outline = JObj.$c("DIV");
            objs.body.appendChild(objs.outline);
            objs.outline.className = vars.skin;
            objs.outline.style.width = vars.width;
            //objs.outline.style.height = vars.height;
            JObj.Style.addRule('.' + vars.skin + ' .panel .ctx','height:' + vars.height + 'px');
            vars.panelHeight = vars.height;
        }

        
        /*       
 		var calcPanelHeight = function(){
            var pan,i=0,h=0;
            for(i=0;pan = objs.labels[i];i++){
                //pan._topT = h;
                h += pan.offsetHeight;
            	}

            h = objs.outline.clientHeight - h;
            JObj.Style.addRule('.' + vars.skin + ' .panel .ctx','height:' + h + "px");
            vars.panelHeight = h;
        }*/

        	var lab_onclick = function(){
            self.open(this.index);
        	}

        var lab_onmouseover = function(){
            vars.mouseoverInterval = setTimeout(JObj.doFunction(self.open,this.index),2);
        }

        self.addPanel = function(label,content){
            var panel = JObj.$c('DIV');
            objs.outline.appendChild(panel);
            panel.className = "panel";

            var lab = JObj.$c("DIV");
            panel.appendChild(lab);
            lab.index = objs.labels.length;
            objs.labels.push(lab);
            lab.className = "label";
            lab.innerHTML = label;
            lab.onclick = lab_onclick;
            lab.onmouseover = lab_onmouseover;

            var ctx = JObj.$c("DIV");
            panel.appendChild(ctx);
            ctx.index = objs.ctxs.length;
            ctx.className = 'ctx';
            ctx.style.display = "none";
            objs.ctxs.push(ctx);

            content = JObj.$(content);
            content && (ctx.appendChild(content));

            //calcPanelHeight();
        }

        var ani_onprocess = function(obj,fromRule,toRule){
            var h = toRule.height - fromRule.height;
            objs.opened && (objs.opened.style.height = h + "px");
        }

        var ani_oncomplete = function(obj){
            objs.opened = obj;
            vars.opening = false;
        }

        self.open = function(idx){
            if(vars.opening || !objs.ctxs[idx] || objs.ctxs[idx] == objs.opened)
                return;

            clearTimeout(vars.mouseoverInterval);

            vars.opening = true;
            JObj.isFunction(self.onOpen) && self.onOpen(objs.ctxs[idx],objs.opened);
            objs.ctxs[idx].style.display = '';
            var ani = JObj.UI.JAnimate.$(objs.ctxs[idx]);
            ani.onProcess = ani_onprocess;
            ani.onComplete = ani_oncomplete;
            ani.run({height:0,alpha:0},{height:vars.panelHeight,alpha:100});
        }

        self.create = function(){
            createOutline();
        }

        self.setSkin = function(skin){
            objs.outline && (objs.outline.className = skin);
            vars.skin = skin;
        }
    }

    $.getInstance = function(body,width,height){
        JObj.Loader.loadCss(JObj.path + '/plugins/JAccordion/default/default.css');
        var ja = new JAccordion(body,width,height);
        ja.create();
        return ja;
    }

})(JObj.Plugin.JAccordion);
